<?php
/**
 * Created by PhpStorm.
 * User: wpf
 * Date: 2018/4/12
 * Time: 11:20
 */

namespace Cardlevel\Controller;

use Common\Controller\AdminbaseController;


class SettingController extends AdminbaseController
{

    public function index(){
        $where = array();

        $id = I('id',-1,'intval');
        $cardname = I('cardname','','trim');
        $status = I('status',-1,'intval');

        if($id>0){
            $where['id'] = $id;
        }else{
            $id = '';
        }

        if($cardname){
            $where['cardname'] = array('like', '%'.$cardname.'%');
        }

        if($status>0){
            $where['status'] = $status;
        }



        $count = D('cardlevel')->where($where)->count();

        $page = $this->page($count,20);

        $list = D('cardlevel')->where($where)->limit($page->firstRow.','.$page->listRows)->select();

        $this->assign('id',$id);
        $this->assign('cardname',$cardname);
        $this->assign('status',$status);
        $this->assign('lists',$list);
        $this->assign('Page',$page->show('Admin'));

        $this->display();
    }

    public function del(){

        $use = I('use',-1,'intval');
        $id = I('id',-1,'intval');

        if($id>0){

        }else{
            $this->error('id不正确！');
        }

        if($use>0){

                $data=array(
                    'id' => $id,
                    'status' => $use
                );

            if(M('cardlevel')->save($data)){
                $this->success('更改成功！');
            }else{
                $this->error('更正失败！');
            }
        }else{
            $this->error("类型不正确！");
        }

    }

    public function edit(){
        if(empty($_POST)){
            $id = I('id',-1,'intval');

            if($id>0){
                $list = M('cardlevel')->find($id);

                $this->assign('list',$list);
                $this->display();
            }
        }else{
            $id = I('id',0,'intval');
            $cardname = I('cardname','','string');
            $sellprice = I('sellprice','0','int');
            $usetime = I('usetime',0,'int');
            $usedanwei = I('usedanwei','','string');
            $initprice = I('initprice','0','int');
            $remark = I('remark','','string');


            if(empty($cardname)){
                $this->error('请输入级别名称！');
            }

            $data = array(
                'id'=>$id,
                'cardname' => $cardname,
                'sellprice'=>$sellprice,
                'usetime'=>$usetime,
                'usedanwei'=>$usedanwei,
                'initprice'=>$initprice,
                'remark'=>$remark
            );

            if(M('cardlevel')->save($data)){
                $this->success('修改成功！',UU('Setting/index'));
            }else{
                $this->error('修改失败！');
            }


        }




    }

    public function add(){

        if(empty($_POST)){
            $this->display();
        }else{
            $cardname = I('cardname','','string');
            $sellprice = I('sellprice','0','int');
            $usetime = I('usetime',0,'int');
            $usedanwei = I('usedanwei','','string');
            $initprice = I('initprice','0','int');
            $remark = I('remark','','string');


            if(empty($cardname)){
                $this->error('请输入级别名称！');
            }

            $data = array(
                'cardname' => $cardname,
                'sellprice'=>$sellprice,
                'usetime'=>$usetime,
                'usedanwei'=>$usedanwei,
                'initprice'=>$initprice,
                'remark'=>$remark,
                'createtime'=>date('Y-m-d H:i:s')
            );

            if(M('cardlevel')->add($data)){
                $this->success('添加成功',UU('Setting/index'));
            }


        }


    }


}